package br.ufmg.vod.collections;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

import org.apache.commons.collections15.iterators.ArrayIterator;

public class IntArrayWrapper implements Collection<Integer>{

	int[] array;
	public IntArrayWrapper(int[] array) {
		this.array = array;
	}

	@Override
	public boolean add(Integer e) {
		throw new UnsupportedOperationException(); 
	}

	@Override
	public boolean addAll(Collection<? extends Integer> c) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void clear() {
		throw new UnsupportedOperationException();		
	}

	@Override
	public boolean contains(Object o) {
		Integer v = (Integer) o;
		return Arrays.binarySearch(array, v) != -1;
	}

	@Override
	public boolean containsAll(Collection<?> c) {
		throw new UnsupportedOperationException();
	}

	@Override
	public boolean isEmpty() {
		return array.length == 0;
	}

	@Override
	public Iterator<Integer> iterator() {
		return new ArrayIterator<Integer>(array);
	}

	@Override
	public boolean remove(Object o) {
		throw new UnsupportedOperationException();
	}

	@Override
	public boolean removeAll(Collection<?> c) {
		throw new UnsupportedOperationException();
	}

	@Override
	public boolean retainAll(Collection<?> c) {
		throw new UnsupportedOperationException();
	}

	@Override
	public int size() {
		return array.length;
	}

	@Override
	public Object[] toArray() {
		Object[] a = new Object[array.length];
		for (int i = 0; i < a.length; i++) {
			a[i] = array[i];
		}
//		System.arraycopy(array, 0, a, 0, array.length);
		return a;
	}

	@SuppressWarnings("hiding")
	@Override
	public <Integer> Integer[] toArray(Integer[] a) {
		System.arraycopy(array, 0, a, 0, array.length);
		return a;
	}
	

}
